﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controller;
using Entity;

namespace Web
{
    public partial class ProductDetail : System.Web.UI.Page
    {
        Helper helper = new Helper();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["id"] != null || Request.QueryString["id"]== "0")
                {
                    int productId = Convert.ToInt32(Request.QueryString["id"]);
                    ProductController productController = new ProductController();
                    ProductEntity productEntity = productController.FindProductById(productId);
                    ProductNoLabel.Text = productEntity.No;
                    ProductNameLabel.Text = productEntity.Name;
                    DescLabel.Text = productEntity.Description;
                    TotalStockLabel.Text = productEntity.Stock.ToString();
                    RevLabel.Text = productEntity.Revision;
                    VisibilityCheckBox.Checked = productEntity.Visibility;
                    StockItemController stockItemController = new StockItemController();
                    IList<StockItemEntity> stockItemEntities = stockItemController.FindStockItemByProductId(productId);
                    DetailGridView.DataSource = stockItemEntities;
                    DetailGridView.DataBind();
                    Session["ProductId"] = productId;
                }
                else
                {
                    Session["FlashMessage"] = "Error: Product Id Cannot Be 0 or Empty!";
                    Session["FlashMessageType"] = "error";
                    Response.Redirect("~/ProductList.aspx");
                }
            }
            catch (Exception)
            {
                Session["FlashMessage"] = "Error: Cannot View The Product!";
                Session["FlashMessageType"] = "error";
                Response.Redirect("~/ProductList.aspx");
            }
            

            DeleteButton.Attributes.Add("onclick","javascript:return   confirm( 'Are you sure?')");

            this.PreRender += new EventHandler(UpdateFlashMessage);
        }

        private void UpdateFlashMessage(object sender, EventArgs e)
        {
            helper.ShowFlashMessage(FlashMessageLabel);
        }

        protected void EditButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("EditProduct.aspx?id=" + Session["ProductId"]);
        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {



            try
            {
                int productId = Convert.ToInt32(Session["productId"]);
                ProductController productController = new ProductController();
                ProductEntity productEntity = productController.FindProductById(productId);
                productController.DeleteProduct(productEntity);
                Session["FlashMessage"] = "Success: Delete Product Successful!";
                Session["FlashMessageType"] = "success";
                Response.Redirect("ProductList.aspx",false);
            }
            catch (Exception ex)
            {
                //ExceptionLabel.Text = "This product exsiting in customer PO or vendor PO, can not delete it!";
                Session["FlashMessage"] = "Error: This Product Exsiting In Customer PO Or Vendor PO, Can Not Delete It!";
                Session["FlashMessageType"] = "error";
            }
        }
    }
}